package com.egao.orderB.service.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.DateUtil;
import com.egao.common.core.damon.utils.StringUtil;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.damon.utils.PhoneUtils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.biz.UserBiz;
import com.egao.common.system.entity.User;
//import com.egao.orderB.mapper.DriverMapper;
import com.egao.common.system.service.UserService;
import com.egao.orderB.biz.CustomerBiz;
import com.egao.orderB.entity.Customer;
import com.egao.orderB.entity.Driver;
import com.egao.orderB.service.DriverService;
import org.springframework.stereotype.Service;
import com.egao.orderB.biz.DriverBiz;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
//import javax.transaction.Transactional;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 司机管理服务实现类
 * @author : Doman
 * @date : 2020-06-03 16:58:06
 * @version ：1.0
 */
@Service
public class DriverServiceImpl /*extends ServiceImpl<DriverMapper, Driver> */implements DriverService {

	@Autowired
	private DriverBiz driverBiz;
	@Autowired
	private UserService userService;
	@Autowired
	private UserBiz userBiz;
	@Autowired
	private CustomerBiz customerBiz;

    /**
    @Override
    public PageResult<Driver> listPage(PageParam<Driver> page) {
        List<Driver> records = baseMapper.listPage(page);
        return new PageResult<>(records, page.getTotal());
    }

    @Override
    public List<Driver> listAll(Map<String, Object> page) {
        return baseMapper.listAll(page);
    } */

    @Override
    public JsonResult list(PageResult2<T> page, Driver driver, User loginUser)  throws Exception{
        return driverBiz.list(page,driver,loginUser);
    }

    @Override
    public JsonResult list(Driver driver, User loginUser) {
        List<Driver> list = driverBiz.list(driver,loginUser);
        for(Driver f: list) {
            if(f.getPinyin()==null || f.getPinyin().equals(""))
                driverBiz.updatePinYin(f,loginUser);
        }
        return JsonResult.ok().setData(driverBiz.list(driver,loginUser));
    }


    @Override
    public Driver getById(Driver driver) {
        return driverBiz.getById(driver);
    }

   /* @Transactional
    @Override
    public JsonResult save(Driver driver, User loginUser) {
        if(StringUtil.isBlank(driver.getWhiteCardDate(),driver.getVehicleLicenseDate(),driver.getDriveLicenseDate(),driver.getOperationLicenseDate(),driver.getInsuranceLicenseDate())) {
            driver.setType("A");
        } else {
            driver.setType("B");
        }
        if (driverBiz.save(driver, loginUser)) {
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }*/

    @Transactional
    @Override
    public JsonResult platsave(Driver driver,User user, User loginUser) throws Exception{
        if(Utils.isEmpty(driver.getUsername())||Utils.isEmpty(driver.getPhone())
        ){
            return JsonResult.error("请填写完整,操作失败!");
        }
        driver.setOrganizationId(UserConstant.ORGANIZATION_ID + "");
        driver.setState("已审核");
        if(Utils.isEmpty(driver.getPhone())){
            driver.setPhone(user.getPhone());
        }
        if(Utils.isEmpty(user.getPhone())){
            driver.setPhone(driver.getPhone());
        }
        if(StringUtil.isBlank(driver.getWhiteCardDate(),driver.getVehicleLicenseDate(),driver.getDriveLicenseDate(),driver.getOperationLicenseDate(),driver.getInsuranceLicenseDate())) {
            driver.setType("A");
        } else {
            driver.setType("B");
        }
        int key= driverBiz.apiSave(driver, loginUser);
        user.setOrganizationId(UserConstant.ORGANIZATION_ID);
        user.setCustomerId(key);
        user.setType("司机");
        user.setAdmin("0");
        user.setState("0");
        String pwd = user.getPassword();
        if(Utils.isEmpty(user.getPassword())) {
            pwd = UserConstant.DEFAULT_PSW;
            user.setPasswordorigin(pwd);
            user.setPassword(userService.encodePsw(UserConstant.DEFAULT_PSW));
        }
        user.setNickName(driver.getName());
        key= userService.apiSave(user);
        user = new User();
        user.setUserId(key);
        user.setRoleId(UserConstant.DRIVER_ROLE_ID);
        userService.saveUserRole(user);
        PhoneUtils.sendMsg(driver.getPhone(), "637388", new String[]{user.getUsername(),pwd},1);
        return JsonResult.ok("注册成功!默认密码为"+pwd).put("date", DateUtil.getCurrentDate());
    }

    @Transactional
    @Override
    public JsonResult excelsave(List<Driver> driverList, User loginUser) throws Exception{
        for(Driver driver: driverList) {
            Customer customer = new Customer();
            customer.setName(driver.getCarTeam());
            customer.setType("车队");
            String customerId = customerBiz.getIdByName(customer).getId();
            if (Utils.isEmpty(customerId)) {
//                return JsonResult.error("请填写正确的车队名"+driver.getCarTeam());
                throw new Exception("请填写正确的车队名"+driver.getCarTeam());
            }
            if(StringUtil.isBlank(driver.getWhiteCardDate(),driver.getVehicleLicenseDate(),driver.getDriveLicenseDate(),driver.getOperationLicenseDate(),driver.getInsuranceLicenseDate())) {
                driver.setType("A");
            } else {
                driver.setType("B");
            }
            driver.setCarTeamId(customerId);
            driver.setOrganizationId(UserConstant.ORGANIZATION_ID + "");
            driver.setState("已审核");
            int key= driverBiz.apiSave(driver, loginUser);
            User user = new User();
            user.setUsername(driver.getUsername());
            user.setNickName(driver.getName());
            user.setPhone(driver.getPhone());
            user.setOrganizationId(UserConstant.ORGANIZATION_ID);
            user.setCustomerId(key);
            user.setType("司机");
            user.setAdmin("0");
            user.setState("0");
            user.setPasswordorigin(UserConstant.DEFAULT_PSW);
            user.setPassword(userService.encodePsw(UserConstant.DEFAULT_PSW));
            key= userService.apiSave(user);
            user = new User();
            user.setUserId(key);
            user.setRoleId(UserConstant.DRIVER_ROLE_ID);
            userService.saveUserRole(user);
        }
        return JsonResult.ok("注册成功!默认密码为123123").put("date", DateUtil.getCurrentDate());
    }
//
//    @Transactional
//    @Override
//    public JsonResult apiSaveUserFirst(Driver driver, User loginUser, User user) {
//        driver.setOrganizationId(UserConstant.ORGANIZATION_ID+"");
//        driver.setState("未审核");
//        int key= driverBiz.apiSave(driver, loginUser);
//        user.setCustomerId(key);
//        userService.apiSetCustomerId(user, loginUser);
//        return JsonResult.ok("添加成功");
//    }

    @Transactional
    @Override
    public JsonResult apiSave(Driver driver, User loginUser, User user) {
        if(Utils.isEmpty(driver.getName()) || Utils.isEmpty(driver.getCarNo())
                || Utils.isEmpty(driver.getIdCard())|| Utils.isEmpty(user.getUsername())
                || Utils.isEmpty(user.getPassword())
        ){
            return JsonResult.error("请填写司机信息,操作失败!");
        }
        if(StringUtil.isBlank(driver.getWhiteCardDate(),driver.getVehicleLicenseDate(),driver.getDriveLicenseDate(),driver.getOperationLicenseDate(),driver.getInsuranceLicenseDate())) {
            driver.setType("A");
        } else {
            driver.setType("B");
        }
        driver.setOrganizationId(UserConstant.ORGANIZATION_ID + "");
        driver.setState("未审核");
        if(Utils.isEmpty(driver.getPhone())){
            driver.setPhone(driver.getUsername());
        }
        int key= driverBiz.apiSave(driver, loginUser);
        if(Utils.isEmpty(user.getPhone())){
            user.setPhone(user.getUsername());
        }
        user.setOrganizationId(UserConstant.ORGANIZATION_ID);
        user.setCustomerId(key);
        user.setType("司机");
        user.setAdmin("0");
        user.setNickName(driver.getName());
        user.setState("2");
        user.setPasswordorigin(user.getPassword());
        user.setPassword(userService.encodePsw(user.getPassword()));
        key= userService.apiSave(user);
        user = new User();
        user.setUserId(key);
        user.setRoleId(UserConstant.DRIVER_ROLE_ID);
        userService.saveUserRole(user);
        return JsonResult.ok("注册成功").put("date", DateUtil.getCurrentDate());
    }

    @Override
    public JsonResult update(Driver driver, User loginUser) {
        if(StringUtil.isBlank(driver.getWhiteCardDate(),driver.getVehicleLicenseDate(),driver.getDriveLicenseDate(),driver.getOperationLicenseDate(),driver.getInsuranceLicenseDate())) {
            driver.setType("A");
        } else {
            driver.setType("B");
        }
        if (driverBiz.update(driver, loginUser)) {
            return JsonResult.ok("修改成功").put("date", DateUtil.getCurrentDate());
        }
        return JsonResult.error("修改失败");
    }
   /* @Override
    public JsonResult driverUpdate(Driver driver, User loginUser) {
        if(StringUtil.isBlank(driver.getWhiteCardDate(),driver.getVehicleLicenseDate(),driver.getDriveLicenseDate(),driver.getOperationLicenseDate(),driver.getInsuranceLicenseDate())) {
            driver.setType("A");
        } else {
            driver.setType("B");
        }
        if (driverBiz.driverUpdate(driver, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }*/


    @Override
    public JsonResult remove(Driver driver, User loginUser) {
        if (driverBiz.remove(driver, loginUser)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

    @Transactional
    @Override
    public JsonResult removeReal(Driver driver) {
        if (driverBiz.removeReal(driver.getId())) {
            User u = new User();
            u.setCustomerId(Integer.valueOf(driver.getId()));
            userBiz.removeReal(u);
            PhoneUtils.sendMsg(driver.getPhone(), "1011273",
                    new String[]{" "+driver.getName()+" "}, 1);
            return JsonResult.ok("驳回成功");
        }
        return JsonResult.error("驳回失败");
    }

    @Transactional
    @Override
    public JsonResult examine(Driver driver, User loginUser) {
        driver.setState("已审核");
        if (driverBiz.examine(driver, loginUser)) {
            User u2 = userService.apiGetByCustomerId(driver.getId(), "司机");
            u2.setState("0");
            userBiz.apiExamineCustomerId(u2, loginUser);

            Driver driver2 = driverBiz.getById(driver);
            PhoneUtils.sendMsg(driver2.getPhone(), "600424", new String[]{u2.getUsername()},1 );
            return JsonResult.ok("审核通过!");
        }
        return JsonResult.error("审核未通过");
    }

}
